package com.sunday.boot3.user.center.consumer;

import com.sunday.boot3.user.center.pojo.dto.permissions.*;
import com.sunday.boot3.user.center.provider.PermissionsProvider;
import com.sunday.common.core.constants.LogConstants;
import com.sunday.common.core.remoting.base.page.PageResultDTO;
import com.sunday.common.core.remoting.base.response.Response;
import com.sunday.common.core.remoting.rpc.response.RPCResponseHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: 权限表 provider 层
 * @author: zsy
 * @Create: 2024/09/20 14:34:16
 */
@Slf4j
@RestController
@RequestMapping("/permissionsProvider")
public class PermissionsConsumer {

    @DubboReference
    private PermissionsProvider permissionsProvider;

    /**
     * @Description: 新增 权限表 数据
     * @Param: [savePermissionsReqDTO]
     * @Return: com.sunday.common.core.remoting.base.response.Response
     * @Author: zsy
     * @Time: 2024/09/20 14:34:16
     */
    @GetMapping("/save")
    public Response save() {
        SavePermissionsReqDTO savePermissionsReqDTO = new SavePermissionsReqDTO();
        savePermissionsReqDTO.setName("savePermissions");
        savePermissionsReqDTO.setCategory(PermissionsConstants.PermissionCategoryEnum.API_PERMISSIONS.getCode());
        savePermissionsReqDTO.setUrl("2333333333");
        savePermissionsReqDTO.setParentId(1L);
        log.info(LogConstants.request_format, savePermissionsReqDTO);

        /**
         * 调用RPC异常应采用代码逻辑实现，可以根据业务及不同得错误码进行区分
         * 仅 provider 进行了dubbo filter 对业务进行一个兜底动作，
         * consumer 不设置filter，请根据业务及报错情况具体分析。
         */
        Response response = RPCResponseHandler.invoke(() -> permissionsProvider.save(savePermissionsReqDTO));

        log.info(LogConstants.response_format_ack, response);

        return response;
    }

    /**
     * @Description: 新增 权限表 数据
     * @Param: [savePermissionsReqDTO]
     * @Return: com.sunday.common.core.remoting.base.response.Response
     * @Author: zsy
     * @Time: 2024/09/20 14:34:16
     */
    @GetMapping("/saveBatch")
    public Response saveBatch() {

        List<SavePermissionsReqDTO> list = Stream.iterate(0, i -> i < 10, i -> i + 1).map(i -> {
            SavePermissionsReqDTO savePermissionsReqDTO = new SavePermissionsReqDTO();
            savePermissionsReqDTO.setName(STR."savePermissions\{i}");
            savePermissionsReqDTO.setCategory(PermissionsConstants.PermissionCategoryEnum.API_PERMISSIONS.getCode());
            savePermissionsReqDTO.setUrl(STR."url\{i}");
            savePermissionsReqDTO.setParentId(1L);
            log.info(LogConstants.request_format, savePermissionsReqDTO);
            return savePermissionsReqDTO;
        }).collect(Collectors.toList());

        SaveBatchPermissionsReqDTO saveBatchPermissionsReqDTO = new SaveBatchPermissionsReqDTO();
        saveBatchPermissionsReqDTO.setPermissionsReqDTOS(list);

        Response response = RPCResponseHandler.invoke(() -> permissionsProvider.saveBatch(saveBatchPermissionsReqDTO));

        log.info(LogConstants.response_format_ack, response);

        return response;
    }

//    /**
//     * @Description: 删除 权限表 数据
//     * @Param: [removePermissionsReqDTO]
//     * @Return: com.sunday.common.core.remoting.base.response.Response
//     * @Author: zsy
//     * @Time: 2024/09/20 14:34:16
//     */
//    Response remove(RemovePermissionsReqDTO removePermissionsReqDTO);
//
//    /**
//     * @Description: 修改 权限表 数据
//     * @Param: [updatePermissionsReqDTO]
//     * @Return: com.sunday.common.core.remoting.base.response.Response
//     * @Author: zsy
//     * @Time: 2024/09/20 14:34:16
//     */
//    Response update(UpdatePermissionsReqDTO updatePermissionsReqDTO);

    /**
     * @Description: 获取 权限表 分页列表数据
     * @Param: [pagePermissionsReqDTO]
     * @Return: com.sunday.common.core.remoting.base.response.Response<com.sunday.common.core.remoting.base.page.PageResultDTO < com.sunday.boot3.user.center.pojo.dto.permissions.PagePermissionsResDTO>>
     * @Author: zsy
     * @Time: 2024/09/20 14:34:16
     */
    @GetMapping("/page")
    public Response<PageResultDTO<PagePermissionsResDTO>> page() {
        PagePermissionsReqDTO pagePermissionsReqDTO = PagePermissionsReqDTO.of(1, 5);

        log.info(LogConstants.request_format, pagePermissionsReqDTO);
        Response<PageResultDTO<PagePermissionsResDTO>> response = RPCResponseHandler.invoke(() -> permissionsProvider.page(pagePermissionsReqDTO));

        log.info(LogConstants.response_format_ack, response);
        return response;
    }

}